﻿using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using WorkLogger.Client.DataAccess.Model;

namespace WorkLogger.Client.DataAccess.Mappings {
	public class WorkDayMap : ClassMapping<WorkDay> {
		public WorkDayMap() {
			Id(workDay => workDay.WorkDayId, m => m.Generator(Generators.Identity));

			Property(workDay => workDay.WorkDate, mapper => {
			                                      	mapper.NotNullable(true);
			                                      	mapper.Unique(true);
			                                      });
			Property(workDay => workDay.StartWork, mapper => mapper.NotNullable(true));
			Property(workDay => workDay.EndWork, mapper => mapper.NotNullable(true));
			Property(workDay => workDay.StartWorkingNotified, mapper => mapper.NotNullable(true));
			Property(workDay => workDay.EndWorkingNotified, mapper => mapper.NotNullable(true));
			Property(workDay => workDay.LastWorkMoment, mapper => mapper.NotNullable(false));

			Bag(workDay => workDay.LunchTimes,
			    mapper => {
			    	mapper.Inverse(true);
			    	mapper.Cascade(Cascade.All | Cascade.DeleteOrphans);
			    	mapper.Lazy(CollectionLazy.NoLazy);
			    	mapper.Key(keyMapper => {
			    	           	keyMapper.Column("WorkDayId");
			    	           	keyMapper.NotNullable(true);
			    	           });
			    },
			    relation => {
			    	relation.OneToMany();
			    	relation.Element(mapper => mapper.NotNullable(true));
			    });
		}
	}
}